package base.filter.lib;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限过滤
 * 
 */
@SuppressWarnings("all")
public class RoleAuthFilter implements Filter {

	public void destroy() {

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HashMap user = (HashMap) req.getSession().getAttribute("user");
		String urlpath = req.getServletPath();
		if (user != null) {
			chain.doFilter(request, response);
		} else if(urlpath.indexOf(".do")>-1){
			// 上下文路径获取
			// ${request.scheme}://${request.serverName}:${request.serverPort}${request.contextPath}
			String basePath = req.getScheme() + "://" + req.getServerName()+ ":" + req.getServerPort() + req.getContextPath();
			res.sendRedirect(basePath+"/admin/login.jsp");
		}else{
			chain.doFilter(request, response);
		}
		
	}

	public void init(FilterConfig config) throws ServletException {

	}

}
